<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Dashboard\Tag;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AutoTag\Action\Dashboard\Traits\AutoContactTag;

/**
 * 批量移除客户企业标签.
 *
 * Class BatchDestroyContactTag
 * @Controller
 */
class BatchDestroyContactTag extends AbstractAction
{
    use ValidateSceneTrait;
    use AutoContactTag;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeContract;

    /**
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeContract;

    /**
     * @var
     */
    protected $contactTag;

    /**
     * @Inject
     * @var WorkContactContract
     */
    private $workContactContract;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/workContactTag/batchDestroyContactTag", methods="PUT")
     */
    public function handle(): array
    {
        $params['tagIds'] = $this->request->input('tagIds');
        $params['list'] = $this->request->input('list');
        // 校验参数
        $this->validated($params);
        foreach ($params['list'] as $item) {
            if ((int) $item['contactId'] === 0) {
                continue;
            }
            $data['tagArr'] = $params['tagIds'];
            $data['contactId'] = (int) $item['contactId'];
            $data['employeeId'] = (int) $item['employeeId'];
            // 客户
            $contact = $this->workContactContract->getWorkContactById((int) $item['contactId'], ['wx_external_userid']);
            if (empty($contact)) {
                continue;
            }
            $data['contactWxExternalUserid'] = $contact['wxExternalUserid'];
            // 员工
            $employee = $this->workEmployeeContract->getWorkEmployeeById((int) $item['employeeId'], ['wx_user_id']);
            if (empty($employee)) {
                continue;
            }
            $data['employeeWxUserId'] = $employee['wxUserId'];
            $data['type'] = 2;
            $this->deleteTag($data);
        }

        return [];
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'tagIds' => 'required|array',
            'list' => 'required|array',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'tagIds.required' => '标签id 必传',
            'list.required' => '客户信息 必传',
        ];
    }
}
